package com.zh.controller;

import com.alibaba.fastjson.JSONObject;
import com.zh.vo.Result;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/4/27/027.
 */
@RestController
@RequestMapping("/m")
public class MessageController {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @RequestMapping("/get")
    public Result getMessage(String user){
        return Result.success(redisTemplate.opsForValue().get(user));
    }


    /**
     * code
     * 0: 没有消息
     * 1:新消息
     * 2:消息已读
     * 30:消息未操作又有新消息
     * 31:消息未操作另外消息已读
     * 310:消息未操作另外消息已读,又有新消息
     * */
    @RequestMapping("/set")
    public Result setMessage(String user,String message){
        //30天
        int exptime = 3600*24*30;
        JSONObject mmp = new JSONObject();
        mmp.put("code", "1");
        mmp.put("message",message);
        redisTemplate.opsForValue().set(user,mmp,exptime, TimeUnit.SECONDS);
        return Result.success();
    }

    @RequestMapping("/look")
    public Result lookMessage(String user){
        //30天
        int exptime = 3600*24*30;
        JSONObject mmp = new JSONObject();
        mmp.put("code", "2");
        mmp.put("message",((JSONObject)redisTemplate.opsForValue().get(user)).get("message").toString());
        redisTemplate.opsForValue().set(user,mmp,exptime, TimeUnit.SECONDS);
        return Result.success();
    }

    @RequestMapping("/edit")
    public Result editMessage(String user){
        //30天
        int exptime = 3600*24*30;
        JSONObject mmp = new JSONObject();
        mmp.put("code", "0");
        mmp.put("message",((JSONObject)redisTemplate.opsForValue().get(user)).get("message").toString());
        redisTemplate.opsForValue().set(user,mmp,exptime, TimeUnit.SECONDS);
        return Result.success("操作成功");
    }

//    @Autowired
//    private AlarmProducer alarmProducer;

    @RequestMapping(value="/message",method= RequestMethod.GET)
    @ApiOperation(value="触发报警", notes="触发报警")
    public String chufabaojing(String devicename){

        List<String> alarmStrList = new ArrayList<String>();
        alarmStrList.add(devicename+"out fence01");
        alarmStrList.add(devicename+"out fence02");
        alarmStrList.add(devicename+"in fence01");
        alarmStrList.add(devicename+"in fence02");

        System.out.println("设备"+devicename+"出围栏报警");
        // 报警信息写入数据库
        System.out.println("报警数据写入数据库。。。");

        // 写入消息队列
//        Destination destination = new ActiveMQQueue("mytest.queue");
//        for (String alarmStr : alarmStrList) {
////            alarmProducer.sendMessage(destination, alarmStr);
//        }

        // 消息写进消息队列里就不管了

        // 下面两步骤移到activemq消费者里
        // 发送邮件
        // 发送短信

        return "success";
    }
}
